package com.team12.hotel404.controller.admin;

import com.team12.hotel404.domain.Order;
import com.team12.hotel404.domain.Hotel;
import com.team12.hotel404.domain.Room;
import com.team12.hotel404.domain.User;
import com.team12.hotel404.service.HotelService;
import com.team12.hotel404.service.OrderService;
import com.team12.hotel404.service.RoomService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.stream.events.Comment;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/room")
public class RoomController {
    @Autowired
    private OrderService orderService;


    @Autowired
    private RoomService roomService;

    @Autowired
    private HotelService hotelService;

    @GetMapping(value = "/list")
    public String list_all_room(Model model) {
        List<Room> rooms = roomService.listAllRoom();
        model.addAttribute("rooms", rooms);
        return "admin/room-list";
    }

    @GetMapping(value="/SearchByRoomType")
    public String preSearch(){
        return "search/roombytype";
    }

    @PostMapping(value = "/listbyroomtype")
    public String list_by_room_type(String roomType, Model model){   //通过房间类型搜索
        List<Room> rooms = roomService.listByRoomType(roomType);
        model.addAttribute("rooms",rooms);
        return "search/roomlist";
    }

    @RequestMapping("findById")
    public String find_by_id(String id,Model model){
        Room room = roomService.findRoomById(id);
        model.addAttribute("room",room);
        return "/admin/room-update";
    }

    @RequestMapping("updateRoom")
    public String updateRoom(Room room){
        roomService.updateRoomInfo(room);
        return "list页面";
    }

    @GetMapping(value="/SearchByCity")
    public String preList(){
        return "search/roombycity";
    }

    @PostMapping(value = "/listbycity")
    public String list_by_area(@Param("cmbArea") String cmbArea,@Param("cmbCity") String cmbCity,@Param("cmbProvince")String cmbProvince, Model model) {
        //通过城市搜索
        String addr=cmbProvince+cmbCity;
        List<Room> rooms = roomService.listByArea(cmbArea);
        model.addAttribute("rooms",rooms);
        model.addAttribute("addrs",addr);
        return "search/roomlist";
    }


    @PostMapping(value = "/listbyhotelname") //通过酒店名称 支持模糊搜索
    public String list_by_hotel(String hotelName, Model model) {//通过酒店搜索
        List<Room> rooms = roomService.listByHotelName(hotelName);
        model.addAttribute("rooms", rooms);
        return "search/roomlist";
    }

    @GetMapping(value="/searchRoom")
    public String roomSearch(){
        return "search/searchroom";
    }

    @GetMapping(value = "/show")
    public String roomPage(Model model, String id){

        Room room= (Room) roomService.findRoomById(id);
        model.addAttribute("room",room);

        return "Page/1room1";


    }





    @RequestMapping(value = "/book")
    @ResponseBody
    public Integer bookPage(@RequestParam("id") String id,HttpSession session, Model model, @RequestParam("Day") Integer Day, @RequestParam("roomPrice") Double roomPrice){

        System.out.println(Day);
        System.out.println(roomPrice);
        //计算房价
        Double amount = Day*roomPrice;
        BigDecimal amount1 = BigDecimal.valueOf(amount);
        //获取用户ID
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        String userPhone = user.getUserPhone();

        //查找酒店ID
        Room showOneRoom = roomService.findHotelIdByRoomId(id);
        Integer hotelId = showOneRoom.gethId();


        System.out.println(id);


        Order order1 = new Order();
        //订单内容
        order1.setOrderDesc("wait..");
        //订单支付状态
        order1.setOrderPayStatus(0);
        //订单金额
        order1.setOrderPayment(amount1);
        //发票类型
        order1.setInvoiceType(1);
        //订单状态
        order1.setOrderStatus(0);
        //下单时间
//        order1.setOrderReserve(null);
//        //预计入住时间
//        order1.setOrderInTime(null);

        //用户ID
        order1.setuId(userId);
        //房间ID
        order1.setrId(id);
        //酒店ID
        order1.sethId(hotelId);
        //用户电话
        order1.setuPhone(userPhone);



        orderService.insertOrder(order1);
        return 1;
    }

    @RequestMapping("/toBookingForm")
    public String toBookingForm(@RequestParam("id") String id, Model model) {
        Integer findId = orderService.selectLastInsertId();
        Order order = orderService.findOrderById(findId);

        Room room=roomService.findRoomById(id);
        model.addAttribute("room",room);
        model.addAttribute("order",order);
        return "Page/bookingform";
    }

    @GetMapping(value = "/payOK")
    public String payOk(Integer HotelId){
        hotelService.salesAdd(HotelId);
        return "/user/hotelIndex";
    }
}